﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class PostAddressEdit : AdminPage
    {
        BLL.CNVP_PostAddress bll = new BLL.CNVP_PostAddress();
        Model.CNVP_PostAddress model = new Model.CNVP_PostAddress();
        public int id = 0;
        public string Province = "0", City = "0", Area = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 98)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                ProvinceBind();
                if (id > 0)
                {
                    lbTitle.Text = " > 修改";
                    DataInfo();
                }
                else
                {
                    lbTitle.Text = " > 添加";
                }
            }
            ddlProvince.Attributes.Add("onchange", "ProvinceChange()");
        }
        private void DataInfo()
        {
            model = bll.GetSingle_CNVP_PostAddressById(id);
            if (model != null)
            {
                txtPostTitle.Text = model.PostTitle;
                txtPostTrueName.Text = model.PostTrueName;
                txtPostAddress.Text = model.PostAddress;
                txtMobilePhone.Text = model.MobilePhone;
                txtPhone.Text = model.Phone;
                txtZipCode.Text = model.ZipCode;
                txtText.Text = model.Text;
                ddlProvince.SelectedValue = model.Province;
                Province = model.Province;
                City = model.City;
                Area = model.Area;
                rblSex.SelectedValue = model.Sex.ToString();
                if (model.IsTolerant == 1)
                {
                    cbIsTolerant.Checked = true;
                }
                else
                {
                    cbIsTolerant.Checked = false;
                }
            }
        }
        private void ProvinceBind()
        {
            IList<Model.CNVP_Province> model = new BLL.CNVP_Province().GetAll_CNVP_Province();
            foreach (Model.CNVP_Province p in model)
            {
                ddlProvince.Items.Add(new ListItem(p.Name, p.Code));
            }
        }

        protected void btSaveAddress_Click(object sender, EventArgs e)
        {
            try
            {
                model.PostTitle = txtPostTitle.Text;
                model.Province = ddlProvince.SelectedValue;
                model.City = Request.Form["ListCity"];
                model.Area = Request.Form["ListArea"];
                model.PostAddress = txtPostAddress.Text;
                model.PostTrueName = txtPostTrueName.Text;
                model.MobilePhone = txtMobilePhone.Text;
                model.Phone = txtPhone.Text;
                model.ZipCode = txtZipCode.Text;
                model.Text = txtText.Text;
                model.Sex = byte.Parse(rblSex.SelectedValue);
                if (cbIsTolerant.Checked == true)
                {
                    bll.Update_CNVP_PostAddressWhereOne();
                    model.IsTolerant = 1;
                }
                else
                {
                    model.IsTolerant = 0;
                }
                if (id > 0)
                {
                    model.ID = id;
                    bll.Update_CNVP_PostAddress(model);
                    BasePage.MessageBox("修改成功！", "PostAddressList.aspx?Page=" + Request.QueryString["Page"] + "");
                    DataInfo();
                }
                else
                {
                   
                    bll.Add_CNVP_PostAddress(model);
                    BasePage.MessageBox("添加成功！", "PostAddressList.aspx");
                }
            }
            catch
            {
                BasePage.MessageBox("失败！");
            }

        }
    }
}